﻿using System;
using System.Collections.Generic;
using System.Text;
using PreeRegisterModel;
using System.Data;
using PreeRegisterDAL;
using Newtonsoft.Json.Linq;
using System.Web;

namespace PreeRegisterBLL
{
    public class DoctorUpdateBLL
    {
        #region 长治和平
        /// <summary>
        /// 医生更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static DoctorUpdateResponseBodyModel DoctorUpdate(DoctorUpdateRequestBodyModel request)
        {
            try
            {
                //获取医生更新信息
                DoctorUpdateResponseBodyModel doctorUpdateResponseBodyModel = GetDoctorUpdate(request);

                return doctorUpdateResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }

        /// <summary>
        /// 医生更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private static DoctorUpdateResponseBodyModel GetDoctorUpdate(DoctorUpdateRequestBodyModel request)
        {
            try
            {
                //获取医生更新情况
                DoctorUpdateResponseBodyModel doctorUpdateResponseBodyModel = new PreeRegisterModel.DoctorUpdateResponseBodyModel();

                //获取所有科室 遍历科室，获取科室下的医生
                DeptUpdateRequestBodyModel model = new DeptUpdateRequestBodyModel();
                DeptUpdateResponseBodyModel deptUpdateResponseBodyModel = DeptUpdateBLL.GetDeptUpdate(model);
                foreach (DeptUpdateModel dept in deptUpdateResponseBodyModel.depts)
                {
                    List<DoctorUpdateModel> DoctorUpdateModel_list = new List<DoctorUpdateModel>();
                    string url_doctor = DrSoftsFrame.BLL.CommonBLL.InCommon.ReadConfig("CZHP_ADDR", "") + "GetOutpatientDoctors?parameters=";
                    StringBuilder head = new StringBuilder();
                    StringBuilder body = new StringBuilder();
                    head.Append("{head:\"\"");
                    body.Append("{");
                    body.AppendFormat("\"DepartmentCode\":\"{0}\",", dept.id.ToString());
                    body.Append("\"Date\":\"\"");
                    body.Append("}");

                    string postData_doctor = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                    postData_doctor += "\"}";
                    url_doctor += postData_doctor;
                    JObject obj_doctor = CommonBLL.HttpPost_CZHP(url_doctor);
                    string code_doctor = obj_doctor["code"].ToString();
                    if (string.Equals(code_doctor, "1"))
                    {
                        var mJObj_doctor = JArray.Parse(obj_doctor["result"].ToString());
                        foreach (var dr in mJObj_doctor)
                        {
                            if (string.Equals(request.id, dr["DoctorCode"].ToString()))
                            {
                                DoctorUpdateModel doctor = new DoctorUpdateModel();
                                doctor.id = dr["DoctorCode"].ToString();
                                doctor.name = dr["Name"].ToString();
                                doctor.did = dept.id;
                                doctor.dname = dept.name;
                                doctor.gender = "";
                                doctor.rank = dr["Rank"].ToString() == "" ? "" : dr["Rank"].ToString();
                                doctor.randid = "";
                                doctor.wrank = dr["Rank"].ToString() == "" ? "" : dr["Rank"].ToString();
                                doctor.info = "";
                                doctor.kword = "";
                                doctor.picurl = "";

                                DoctorUpdateModel_list.Add(doctor);
                                doctorUpdateResponseBodyModel.hospid = VariableBLL.HospitalId;
                                doctorUpdateResponseBodyModel.doctors = DoctorUpdateModel_list;
                                return doctorUpdateResponseBodyModel;
                            }
                        }
                    }
                }
                //返回
                return doctorUpdateResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }


        /// <summary>
        /// 根据科室ID获取科室下所有的医生
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static DoctorUpdateResponseBodyModel GetDoctorUpdate(string deptId)
        {
            try
            {
                //获取医生更新情况
                DoctorUpdateResponseBodyModel doctorUpdateResponseBodyModel = new PreeRegisterModel.DoctorUpdateResponseBodyModel();

                //获取所有科室 遍历科室，获取科室下的医生
                DeptUpdateRequestBodyModel model = new DeptUpdateRequestBodyModel();

                List<DoctorUpdateModel> DoctorUpdateModel_list = new List<DoctorUpdateModel>();
                string url_doctor = CommonBLL.GetConfig("CZHP_ADDR") + "GetOutpatientDoctors?parameters=";
                StringBuilder head = new StringBuilder();
                StringBuilder body = new StringBuilder();
                head.Append("{head:\"\"");
                body.Append("{");
                body.AppendFormat("\"DepartmentCode\":\"{0}\",", deptId);
                body.Append("\"Date\":\"\"");
                body.Append("}");

                string postData_doctor = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                postData_doctor += "\"}";
                url_doctor += postData_doctor;
                CommonBLL.WriteFile_New(CommonBLL.bizCode_workUpdate,
                string.Format("排班更新接口，获取下的医生，科室ID：{0}\r\n，获取医生URL：{1}", deptId, url_doctor));
                JObject obj_doctor = CommonBLL.HttpPost_CZHP(url_doctor);
                string code_doctor = obj_doctor["code"].ToString();
                if (string.Equals(code_doctor, "1"))
                {
                    var mJObj_doctor = JArray.Parse(obj_doctor["result"].ToString());
                    foreach (var dr in mJObj_doctor)
                    {
                        DoctorUpdateModel doctor = new DoctorUpdateModel();
                        doctor.id = dr["DoctorCode"].ToString();
                        doctor.name = dr["Name"].ToString();
                        doctor.did = deptId;
                        doctor.dname = dr["Department"]["Name"].ToString();
                        doctor.gender = "";
                        doctor.rank = dr["Rank"].ToString() == "" ? "" : dr["Rank"].ToString();
                        doctor.randid = "";
                        doctor.wrank = dr["Rank"].ToString() == "" ? "" : dr["Rank"].ToString();
                        doctor.info = "";
                        doctor.kword = "";
                        doctor.picurl = "";
                        DoctorUpdateModel_list.Add(doctor);
                    }
                    doctorUpdateResponseBodyModel.hospid = VariableBLL.HospitalId;
                    doctorUpdateResponseBodyModel.doctors = DoctorUpdateModel_list;
                }
                //返回
                return doctorUpdateResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }


        public static DoctorUpdateResponseBodyModel GetDoctorUpdate_Test(string deptId)
        {
            try
            {
                //获取医生更新情况
                DoctorUpdateResponseBodyModel doctorUpdateResponseBodyModel = new PreeRegisterModel.DoctorUpdateResponseBodyModel();

                //获取所有科室 遍历科室，获取科室下的医生
                DeptUpdateRequestBodyModel model = new DeptUpdateRequestBodyModel();

                List<DoctorUpdateModel> DoctorUpdateModel_list = new List<DoctorUpdateModel>();

                if (1 == 1)
                {
                    //DoctorUpdateModel doctor = new DoctorUpdateModel();
                    //doctor.id = "0009";
                    //doctor.name = "王治平";
                    //doctor.did = "001";
                    //doctor.dname = "心血管科";
                    //doctor.gender = "";
                    //doctor.rank = "";
                    //doctor.randid = "";
                    //doctor.wrank = "";
                    //doctor.info = "";
                    //doctor.kword = "";
                    //doctor.picurl = "";
                    //DoctorUpdateModel_list.Add(doctor);

                    DoctorUpdateModel doctor2 = new DoctorUpdateModel();
                    doctor2.id = "0147";
                    doctor2.name = "郭先菊";
                    doctor2.did = "018";
                    doctor2.dname = "心理门诊";
                    doctor2.gender = "";
                    doctor2.rank = "";
                    doctor2.randid = "";
                    doctor2.wrank = "";
                    doctor2.info = "";
                    doctor2.kword = "";
                    doctor2.picurl = "";
                    DoctorUpdateModel_list.Add(doctor2);

                    doctorUpdateResponseBodyModel.hospid = VariableBLL.HospitalId;
                    doctorUpdateResponseBodyModel.doctors = DoctorUpdateModel_list;
                }
                //返回
                return doctorUpdateResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取科室信息 暂时不用
        /// </summary>
        /// <returns></returns>
        public static DeptUpdateResponseBodyModel GetDeptUpdate(DeptUpdateRequestBodyModel request, bool isGetDoctor, out DoctorUpdateResponseBodyModel doctorInfoRequestModel)
        {
            try
            {
                doctorInfoRequestModel = new DoctorUpdateResponseBodyModel();
                string url = CommonBLL.GetConfig("CZHP_ADDR") + "GetOutpatientDepartments?parameters=";
                string postData = "{head:\"\",body:\"\"}";
                url += postData;
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "2：获取科室信息接口请求密文：\r\n" + url);
                JObject obj = CommonBLL.HttpPost_CZHP(HttpUtility.UrlEncode(url));
                CommonBLL.WriteFile_New(CommonBLL.bizCode_deptUpdate, "3：获取科室信息接口返回明文：\r\n" + CommonBLL.ConvertJsonString(obj.ToString()));
                DeptUpdateResponseBodyModel deptInfoRequestModel = new DeptUpdateResponseBodyModel();
                deptInfoRequestModel.hospid = VariableBLL.HospitalId;
                List<DeptUpdateModel> depts = new List<DeptUpdateModel>();
                string code = obj["code"].ToString();
                if (string.Equals(code, "1"))
                {
                    var mJObj = JArray.Parse(obj["result"].ToString());
                    foreach (var dr in mJObj)
                    {
                        //查询所有科室信息，通过ID匹配返回请求的科室信息
                        if (string.Equals(request.id, dr["DepartmentCode"].ToString()))
                        {
                            DeptUpdateModel dept = new DeptUpdateModel();
                            dept.id = dr["DepartmentCode"].ToString();
                            dept.name = dr["Name"].ToString();
                            dept.tel = "";
                            depts.Add(dept);
                        }
                    }
                    deptInfoRequestModel.depts = depts;
                    if (isGetDoctor)
                    {
                        doctorInfoRequestModel.hospid = VariableBLL.HospitalId;
                        List<DoctorUpdateModel> doctors = new List<DoctorUpdateModel>();
                        foreach (DeptUpdateModel dept in deptInfoRequestModel.depts)
                        {
                            string url_doctor = DrSoftsFrame.BLL.CommonBLL.InCommon.ReadConfig("CZHP_ADDR", "") + "GetOutpatientDoctors?parameters=";
                            StringBuilder head = new StringBuilder();
                            StringBuilder body = new StringBuilder();
                            head.Append("{head:\"\"");
                            body.Append("{");
                            body.AppendFormat("\"DepartmentCode\":\"{0}\",", dept.id.ToString());
                            body.Append("\"Date\":\"\"");
                            body.Append("}");

                            string postData_doctor = string.Format("{0},body:\"{1}", head.ToString(), HttpUtility.UrlEncode(Security.Base64.EncodeBase64(body.ToString())));
                            postData_doctor += "\"}";
                            url_doctor += postData_doctor;
                            JObject obj_doctor = CommonBLL.HttpPost_CZHP(url_doctor);
                            string code_doctor = obj_doctor["code"].ToString();
                            if (string.Equals(code_doctor, "1"))
                            {
                                var mJObj_doctor = JArray.Parse(obj_doctor["result"].ToString());
                                foreach (var dr in mJObj_doctor)
                                {
                                    DoctorUpdateModel doctor = new DoctorUpdateModel();
                                    doctor.id = dr["DoctorCode"].ToString();
                                    doctor.name = dr["Name"].ToString();
                                    doctor.did = dept.id;
                                    doctor.dname = dept.name;
                                    doctor.gender = "";
                                    doctor.rank = dr["Rank"].ToString() == "" ? "" : dr["Rank"].ToString();
                                    doctor.randid = "";
                                    doctor.wrank = "";
                                    doctor.info = "";
                                    doctor.kword = "";
                                    doctor.picurl = "";

                                    doctors.Add(doctor);
                                }
                                doctorInfoRequestModel.doctors = doctors;
                            }
                        }

                    }
                }
                return deptInfoRequestModel;
            }
            catch (Exception err)
            {
                CommonBLL.WriteFile_New(CommonBLL.bizCode_searchNopay, err.ToString());
                throw;
            }
        }

        #endregion



        /// <summary>
        /// 医生获取
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static DoctorGetResponseBodyModel DoctorGet(DoctorGetRequestBodyModel request)
        {
            try
            {

                //获取医生更新信息
                DoctorGetResponseBodyModel doctorGetResponseBodyModel = GetDoctor(request);

                return doctorGetResponseBodyModel;
            }
            catch (Exception err)
            {
                throw;
            }
        }


        /// <summary>
        /// 医生获取
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        private static DoctorGetResponseBodyModel GetDoctor(DoctorGetRequestBodyModel request)
        {
            try
            {
                //查询医生表
                DoctorGetResponseBodyModel doctorGetResponseBodyModel = new PreeRegisterModel.DoctorGetResponseBodyModel();

                DataSet ds = DoctorUpdateDAL.GetDoctorInfoByDeptId(request.deptid);

                //获取医生更新情况
                List<DoctorGetModel> DoctorGetModel_list = new List<DoctorGetModel>();

                foreach (DataRow drDoctor in ds.Tables["DictStaff"].Rows)
                {
                    DoctorGetModel doctorGetModel = new DoctorGetModel();
                    doctorGetModel.id = drDoctor["no_staff"].ToString();
                    doctorGetModel.name = drDoctor["staffname"].ToString();

                    DoctorGetModel_list.Add(doctorGetModel);
                }

                doctorGetResponseBodyModel.doctors = DoctorGetModel_list;

                //返回
                return doctorGetResponseBodyModel;
            }
            catch (Exception err)
            {

                throw;
            }
        }
    }
}
